#include "BlockManager.h"

// Constructor
BlockManager::BlockManager() {
	this->head = NULL;
}

// Deconstructor
BlockManager::~BlockManager() {
	Block *slider = this->head;

	while(slider) {
		Block *tmp = slider;

		slider = slider->next;

		delete tmp;
	}
}


// Manage a new block
bool BlockManager::add(char *index, int size) {
	Block *addme = new Block(index, size);

	if(this->head == NULL) {
		this->head = addme;
	}
	else {
		addme->next = this->head;
		this->head->prev = addme;
		this->head = addme;
	}

	return true;
}

// Delete a block
bool BlockManager::del(Block *block) {
	Block *slider = this->head;

	while(slider) {
		if(slider == block) {
			// block identified
			if(slider->prev) {
				if(slider->next) {
					slider->prev->next = slider->next;
				}
				else {
					slider->prev->next = NULL;
				}
			}
			if(slider->next) {
				if(slider->prev) {
					slider->next->prev = slider->prev;
				}
				else {
					slider->next->prev = NULL;
				}
			}
			// fix head node if we are deleting it
			if(slider == this->head) {
				this->head = slider->next;
				if(this->head) {
					this->head->prev = NULL;
				}
			}

			// free up resources
			delete slider;

			break;
		}

		slider = slider->next;
	}

	return true;
}


// returns total number of todos in 'pending' state
int BlockManager::getPending() {
	Block *slider = this->head;
	int count = 0;

	while(slider != NULL) {
		count += slider->getPending();

		slider = slider->next;
	}

	return count;
}

// returns total number of todos in 'active' state
int BlockManager::getActive() {
	Block *slider = this->head;
	int count = 0;

	while(slider != NULL) {
		count += slider->getActive();

		slider = slider->next;
	}

	return count;
}

// returns total number of todos in 'complete' state
int BlockManager::getComplete() {
	Block *slider = this->head;
	int count = 0;

	while(slider != NULL) {
		count += slider->getComplete();

		slider = slider->next;
	}

	return count;
}

// whether block is complete
bool BlockManager::isComplete() {
	Block *slider = this->head;

	while(slider != NULL) {
		if(!slider->isComplete()) {
			return false;
		}

		slider = slider->next;
	}

	return true;
}


// returns the next index to start
char* BlockManager::getNextIndex() {
	Block *slider = this->head;

	while(slider != NULL) {
		if(slider->getPending() != 0) {
			return slider->getNextIndex();
		}

		slider = slider->next;
	}

	return NULL;
}

/*
// track the todo thread
bool BlockManager::track(HANDLE todo) {
	Block *slider = this->head;

	while(slider != NULL) {
		if(slider->getPending()) {
			slider->track(todo);
			return true;
		}

		slider = slider->next;
	}

	return false;
}

// updates the status of todos
void BlockManager::update() {
	Block *slider = this->head;

	while(slider != NULL) {
		slider->update();

		slider = slider->next;
	}
}

// Find an active thread and watch it waiting for it to finish, return true
// If unable to find an active thread, return false
bool BlockManager::waitForThread() {
	Block *slider = this->head;

	while(slider != NULL) {
		if(slider->getActive()) {
			return slider->waitForThread();
		}

		slider = slider->next;
	}

	return false;
}
*/